package com.wgchao.identityrightsmanagement.infrastructure.dao;

import com.wgchao.identityrightsmanagement.domain.permission.model.FunctionInformation;
import com.wgchao.identityrightsmanagement.domain.permission.model.OfficePermissionInformation;
import com.wgchao.identityrightsmanagement.domain.permission.model.PermissionIdentify;
import com.wgchao.identityrightsmanagement.domain.permission.repository.OfficePermissionInformationRepository;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.entity.SysPermissionOffice;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.entity.SysPermissionOfficeFunction;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.repository.SysPermissionOfficeFunctionRepository;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.repository.SysPermissionOfficeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @ClassName: OfficePermissionInformationDao
 * @author: wgchao
 * @createTime: 2020/6/28 5:31 PM
 * @Description: 职位授权
 */
@Component
public class OfficePermissionInformationDao implements OfficePermissionInformationRepository {
    @Autowired
    private SysPermissionOfficeRepository permissionOfficeRepository;
    @Autowired
    private SysPermissionOfficeFunctionRepository permissionOfficeFunctionRepository;

    @Override
    public OfficePermissionInformation findById(PermissionIdentify permissionIdentify) {
        return null;
    }

    @Override
    public void remove(OfficePermissionInformation officePermissionInformation, String operator) {

    }

    @Override
    public void add(OfficePermissionInformation officePermissionInformation, String operator) {
        //清除原有的权限信息
        SysPermissionOffice sysPermissionOffice = permissionOfficeRepository
                .findFirstByMenuMarkAndOfficeCode(officePermissionInformation.getIdentify().getMenuMark(),
                        officePermissionInformation.getIdentify().getOfficeCode());
        if(sysPermissionOffice != null){//删除后重新查询-刷新掉缓存
            permissionOfficeFunctionRepository.deleteAllByPid(sysPermissionOffice.getId());
            permissionOfficeFunctionRepository.findAllByPid(sysPermissionOffice.getId());
            permissionOfficeRepository.delete(sysPermissionOffice);
            permissionOfficeRepository.findFirstByMenuMarkAndOfficeCode(officePermissionInformation.getIdentify().getMenuMark(),
                            officePermissionInformation.getIdentify().getOfficeCode());
        }

        //写入新的权限信息
        sysPermissionOffice = new SysPermissionOffice();
        sysPermissionOffice.setMenuMark(officePermissionInformation.getIdentify().getMenuMark());
        sysPermissionOffice.setDepartmentCode(officePermissionInformation.getIdentify().getDepartmentCode());
        sysPermissionOffice.setOfficeCode(officePermissionInformation.getIdentify().getOfficeCode());
        sysPermissionOffice.setProMark(officePermissionInformation.getProMark());
        sysPermissionOffice.setCreator(operator);
        sysPermissionOffice.setCreateTime(new Date());
        sysPermissionOffice = permissionOfficeRepository.save(sysPermissionOffice);

        if(officePermissionInformation.getFunctions() != null && !officePermissionInformation.getFunctions().isEmpty()){
            for(FunctionInformation functionInformation : officePermissionInformation.getFunctions()){
                SysPermissionOfficeFunction permissionOfficeFunction = new SysPermissionOfficeFunction();
                permissionOfficeFunction.setPid(sysPermissionOffice.getId());
                permissionOfficeFunction.setFuncMark(functionInformation.getFuncMark());
                permissionOfficeFunction = permissionOfficeFunctionRepository.save(permissionOfficeFunction);            }
        }

    }

    @Override
    public void cleanUpIllegalCorrespondence(String departmentCode, List<String> officeCodes, String operator) {
        List<SysPermissionOffice> permissionOffices = null;
        if(officeCodes ==null || officeCodes.isEmpty()){
            permissionOffices = permissionOfficeRepository
                    .findAllByDepartmentCode(departmentCode);
        }else{
            permissionOffices = permissionOfficeRepository
                    .findAllByDepartmentCodeAndOfficeCodeNotIn(departmentCode, officeCodes);
        }
        if(permissionOffices != null){
            for(SysPermissionOffice permissionOffice : permissionOffices){
                permissionOfficeFunctionRepository.deleteAllByPid(permissionOffice.getId());
                permissionOfficeRepository.delete(permissionOffice);
            }
        }
    }

    @Override
    public void deleteAllByDepartment(String departmentCode, String operator) {
        List<SysPermissionOffice> permissionOffices = permissionOfficeRepository.findAllByDepartmentCode(departmentCode);
        if(permissionOffices != null){
            for(SysPermissionOffice permissionOffice : permissionOffices){
                permissionOfficeFunctionRepository.deleteAllByPid(permissionOffice.getId());
                permissionOfficeRepository.delete(permissionOffice);
            }
        }
    }

    @Override
    public void deleteAllByOffice(String officeCode, String operator) {
        List<SysPermissionOffice> permissionOffices = permissionOfficeRepository.findAllByOfficeCode(officeCode);
        if(permissionOffices != null){
            for(SysPermissionOffice permissionOffice : permissionOffices){
                permissionOfficeFunctionRepository.deleteAllByPid(permissionOffice.getId());
                permissionOfficeRepository.delete(permissionOffice);
            }
        }
    }

    @Override
    public void addAll(List<OfficePermissionInformation> officePermissionInformations, String operator) {

    }

    @Override
    public void removeAll(List<OfficePermissionInformation> officePermissionInformations, String operator) {

    }
}
